package evi.com.evi.utils;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import evi.com.evi.fragment.DisposeFalseFragment;
import evi.com.evi.fragment.DisposeTrueFragment;
import evi.com.evi.fragment.ManagerFragment;

/**
 * @作者： neil.chen
 * @创建时间： 2016年4月13日下午4:05:45
 * @描述: fragment——radiogroup
 */
public class FragmentTabUtils implements OnCheckedChangeListener {

    private RadioGroup rg;

    private FragmentManager manager;
    private int containerId;
    private List<Fragment> fragmentList;

    public FragmentTabUtils(RadioGroup rg, int contianerId,
                            FragmentManager fragmentManager, List<Fragment> fragmentList) {

        this.rg = rg;
        this.manager = fragmentManager;
        this.fragmentList = fragmentList;
        this.containerId = contianerId;

        for (int i = 0; i < rg.getChildCount(); i++) {
            Fragment fragment = fragmentList.get(i);
            FragmentTransaction trans = manager.beginTransaction();
            trans.add(containerId, fragment, fragment.getClass().getSimpleName());
            trans.hide(fragment).commit();
        }
        rg.setOnCheckedChangeListener(this);

        //设置默认显示
        manager.beginTransaction().show(fragmentList.get(0)).commitAllowingStateLoss();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for (int i = 0; i < rg.getChildCount(); i++) {
            Fragment fragment = fragmentList.get(i);
            View view = rg.getChildAt(i);
            if (view.getId() == checkedId) {
                if (i == 0) {
//                    ((DisposeFalseFragment)fragment).initPager();
//                    ((DisposeTrueFragment)fragment).getInfo();
                } else if (i == 1){
//                    ((DisposeTrueFragment)fragment).getInfo();
                }
                manager.beginTransaction().show(fragment).commitAllowingStateLoss();
//                Log.e("TAG", "++++++++++++++++++: " + i);
            } else {
                manager.beginTransaction().hide(fragment).commitAllowingStateLoss();
            }
        }
    }

    /*
     * 
     */
    public void show(int position) {
        rg.check(((RadioButton) rg.getChildAt(position)).getId());
    }
}
